package com.lixp.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lixp.exam.bean.Msg;
import com.lixp.exam.bean.Student;
import com.lixp.exam.bean.StudentUser;
import com.lixp.exam.bean.UserInfo;
import com.lixp.exam.config.JwtConfig;
import com.lixp.exam.config.PictureConfig;
import com.lixp.exam.service.StudentService;
import com.lixp.exam.utils.JwtUtils;
import com.lixp.exam.utils.RsaUtils;
import com.lixp.exam.utils.UploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    private PictureConfig pictureConfig;

    @Autowired
    private JwtConfig jwtConfig;


    /**
     * 根据考试号获取全部的考生信息
     *
     * @param eno
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/AllStudent/{eno}", name = "admin")
    public Msg getAllStudent(@PathVariable("eno") String eno,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentService.selectAllStudent(eno);
        PageInfo page = new PageInfo(studentList, 5);
        return Msg.success().add("studentList", page);
    }


    /**
     * 添加,更新学生信息
     *
     * @return
     */
    @PostMapping(value = "changeStudent", name = "admin")
    public Msg updateStudent(Student student) {
        Integer sid = student.getSid();
        try {
            if (sid == null) {//sid为空,新增
                studentService.saveStudent(student);//添加考生
            } else {
                studentService.updateStudent(student);//更新考生信息
            }
        } catch (Exception e) {
            return Msg.fail();
        }
        return Msg.success();
    }

    /**
    *@ClassName: StudentController
    *@Description: onePictureUpload方法是单个图片上传,学生
    *@Params:
    *@Return:
    *@Author xxw
    *@Date 2021/4/30
    */

    Logger logger = LoggerFactory.getLogger(StudentController.class);

    @PostMapping(value = "/pictureUpload", name = "user")
    public void pictureUpload(@RequestParam("file") MultipartFile file, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取后缀
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        if (file == null) {
            response.setStatus(HttpStatus.BAD_REQUEST.value());
        } else if (suffixName.equals(".jpg") || suffixName.equals(".png") || suffixName.equals(".jepg")){
            String targetFile = this.pictureConfig.getUrl();//文件存放地址
            // 获取学生身份证
            String usertoken = request.getHeader("USERTOKEN");
            //获取公钥
            PublicKey publicKey = RsaUtils.getPublicKey(jwtConfig.getPublicKeyFilename());
            //解析用户信息，这里没报错的话
            UserInfo userInfo = JwtUtils.getInfoFromToken(usertoken, publicKey);

            // 获取邮箱用于查询对应身份证，给照片命名
            String email = userInfo.getUsername();

            StudentUser studentUser = studentService.selectStuInfoByEmail(email);

            File picFileName = new File(targetFile+File.separator+studentUser.getsCardNo() + suffixName);
            if (picFileName.exists()){
                picFileName.delete();
            }
            try (FileOutputStream out = new FileOutputStream(targetFile + studentUser.getsCardNo() + suffixName)) {
                out.write(file.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("文件上传失败!");
                response.setStatus(HttpStatus.BAD_REQUEST.value());
            }
            logger.info("文件上传成功!");
            studentService.flushPic();//刷新学生的spic地址
            response.setStatus(HttpStatus.OK.value());
        } else {
            response.setStatus(HttpStatus.BAD_REQUEST.value());
        }
    }

   /**
   *@ClassName: StudentController
   *@Description: onePictureUpload方法是单个图片上传,管理员
   *@Params:
   *@Return:
   *@Author xxw
   *@Date 2020/8/17 16:40
   */
    @PostMapping(value = "/onePictureUpload", name = "admin")
    public Msg onePictureUpload(@RequestParam("file") MultipartFile file, HttpSession session, HttpServletRequest request) throws Exception {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取后缀
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        if (file == null) {
            return Msg.fail();
        } else if (suffixName.equals(".jpg") || suffixName.equals(".png") || suffixName.equals(".jepg")){
            String targetFile = this.pictureConfig.getUrl();//文件存放地址
            // 获取学生身份证
            String usertoken = request.getHeader("USERTOKEN");
            //获取公钥
            PublicKey publicKey = RsaUtils.getPublicKey(jwtConfig.getPublicKeyFilename());
            //解析用户信息，这里没报错的话
            UserInfo userInfo = JwtUtils.getInfoFromToken(usertoken, publicKey);

            // 获取邮箱用于查询对应身份证，给照片命名
            String email = userInfo.getUsername();

            StudentUser studentUser = studentService.selectStuInfoByEmail(email);

            File picFileName = new File(targetFile+File.separator+studentUser.getsCardNo() + suffixName);
            if (picFileName.exists()){
                picFileName.delete();
            }
            try (FileOutputStream out = new FileOutputStream(targetFile + studentUser.getsCardNo() + suffixName)) {
                out.write(file.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("文件上传失败!");
                return Msg.fail();
            }
            logger.info("文件上传成功!");
            studentService.flushPic();//刷新学生的spic地址
            return Msg.success()
                    .add("filePath", "http://"+pictureConfig.getIp()+"/EXAM/static/"+ studentUser.getsCardNo() + suffixName)
                    .add("fileName", studentUser.getsCardNo() + suffixName);
        } else {
            return Msg.fail();
        }
    }

/*
*
*  已废弃管理员界面不上传照片
*
* */
    /**
     * 解析学生图片压缩包
     *
     */
//    @PostMapping(value = "/pictureUpload",name = "admin")
//    public Msg uploadPushContent(@RequestParam("file")MultipartFile file, HttpSession session, HttpServletRequest request) {
//        if (file == null) {
//
//        } else {
//            try {
//                //获取存储文件的目录
//                String path=this.pictureConfig.getUrl();//文件存放地址
//
//                String saveFileName = UploadUtil.resolveCompressUploadFile(request, file, path);
//
//               studentService.flushPic();//刷新学生的spic地址
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }
//
//        return Msg.success();
//    }

    /**
     * 解析excel文件
     */
    @PostMapping(value="/StudentUpload",name="admin")
    public Msg fileUpload(@RequestParam("file") MultipartFile file,
                          HttpServletRequest request) throws IOException {

        InputStream is = file.getInputStream();
        try {
            List<Student> list = studentService.getListByExcel(is,file.getOriginalFilename());
            if(studentService.batchInsertStudent(list)){
                studentService.flushPic();//刷新学生的spic地址
                return Msg.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  Msg.fail();
    }
    /**
     * 刷新全部学生的照片信息,将照片路径和身份证关联起来
     */
    @GetMapping(value = "flushStuPic",name="admin")
    public Msg flushStuPic(){

        boolean b = studentService.flushPic();
        if (!b){
            return Msg.fail();
        }
        return Msg.success();
    }

}
